﻿
#If RETIRED Then
Imports System.IO
Imports std

Public Class nettransData
    Implements ICloneable

    Public Const displayRange As UInt32 = 128
    Public ReadOnly sizeofInt64 As Int32 = sizeof(Of Int64)()

    Protected Friend _field As vector(Of bytes)

    Public Function available() As Boolean
        Dim i As Int64
        For i = 0 To _field.size() - 1
            If _field.data(i) Is Nothing Then
                Exit For
            End If
        Next

        If i < _field.size() Then
            Return False
        Else
            Return True
        End If
    End Function

    Public Function insert(ByVal values() As bytes, Optional ByVal start As Int64 = 0 _
                            , Optional ByVal size As Int64 = npos) As Boolean
        Return _field.push_back(values, start, size)
    End Function

    Public Function insert(ByVal value As bytes) As Boolean
        Return _field.push_back(value)
    End Function

    Public Function insert(Of valueT)(ByVal value As valueT) As Boolean
        Dim internal As bytes = New bytes(encoding.utf8.GetBytes(Convert.ToString(value)))
        Return insert(internal)
    End Function

    Public Function take(ByVal index As Int64, ByRef buff As bytes) As Boolean
        buff = Nothing
        If index >= _field.size() Or index < 0 Then
            Return False
        Else
            buff = _field(index)
            Return True
        End If
    End Function

    Public Function take(ByVal index As Int64) As bytes
        Dim field As bytes = Nothing
        take(index, field)
        Return field
    End Function

    Public Function take(ByVal index As Int64, ByRef value As Object) As Boolean
        Dim field As bytes = Nothing
        field = take(index)
        If field Is Nothing Then
            Return False
        Else
            Dim str As String
            If Not field.data Is Nothing Then
                str = encoding.utf8.GetString(field.data, 0, field.size)
                value = str
            End If
            Return True
        End If
    End Function

    Public Function take(Of valueT)(ByVal index As Int64) As valueT
        Dim temp As valueT
        If take(index, temp) Then
            Return temp
        Else
            Return Nothing
        End If
    End Function

    Default Public Property data(ByVal index As Int64) As bytes
        Get
            Return _field(index)
        End Get
        Set(ByVal value As bytes)
            _field.set(index, value)
        End Set
    End Property

    Public Function export(ByRef buff() As Byte, ByVal start As Int64, ByRef size As Int64) As Boolean
        size = 0
        Dim i As Int64
        For i = 0 To _field.size() - 1
            If _field(i) Is Nothing Then
                size += sizeofInt64
            Else
                size += _field(i).size + sizeofInt64
            End If
        Next
        If buff Is Nothing Then
            ReDim buff(start + size - 1)
        ElseIf buff.Length < start + size Then
            ReDim Preserve buff(start + size - 1)
        End If

        Dim mem As MemoryStream = Nothing
        mem = New MemoryStream(buff, start, size)
        Dim writer As BinaryWriter = Nothing
        writer = New BinaryWriter(mem)
        assert(Not writer Is Nothing, "cannot create BinaryWriter and MemoryStream on buff")
        For i = 0 To _field.size() - 1
            If _field(i) Is Nothing Then
                writer.Write(Convert.ToInt64(0))
            Else
                writer.Write(_field(i).size())
                writer.Write(_field(i).data)
            End If
        Next

        assert(writer.BaseStream.Position = start + size, "writer.BaseStream.Position <> start + size")
        closestream(writer)

        Return True
    End Function

    Public Function export(ByRef buff() As Byte, ByRef size As Int64) As Boolean
        Return export(buff, 0, size)
    End Function

    Public Function import(ByVal buff() As Byte, ByVal start As Int64, ByVal size As Int64) As Boolean
        If buff Is Nothing OrElse start + size > buff.Length Then
            Return False
        Else
            Dim mem As MemoryStream = Nothing
            mem = New MemoryStream(buff, start, size)
            Dim reader As BinaryReader = Nothing
            reader = New BinaryReader(mem)
            assert(Not reader Is Nothing, "cannot create BinaryReader and MemoryStream on buff")

            While binaryReaderHelper.readInt64(reader, size)
                insert(New bytes(reader.ReadBytes(size)))
            End While

            closestream(reader)

            Return True
        End If
    End Function

    Public Function import(ByVal buff() As Byte) As Boolean
        If buff Is Nothing Then
            Return False
        End If
        Return import(buff, 0, buff.Length)
    End Function

    Public ReadOnly Property fieldCount() As Int64
        Get
            Return _field.size()
        End Get
    End Property

    Public Sub copy(ByVal value As nettransData)
        _field.clear()
        If Not value Is Nothing Then
            _interfaceOperations.copy(_field, value._field)
        End If
    End Sub

    Public Sub clone(ByVal value As nettransData)
        copy(value)
    End Sub

    Public Sub New()
        clear()
    End Sub

    Public Sub New(ByVal value As nettransData)
        clear()
        clone(value)
    End Sub

    Public Sub clear()
        If _field Is Nothing Then
            _field = New vector(Of bytes)
        Else
            _field.clear()
        End If
    End Sub

    Protected Overrides Sub Finalize()
        clear()
        MyBase.Finalize()
    End Sub

    Public Function display() As String
        Dim rtn As String = Nothing
        Dim i As Int64
        Dim buff As bytes = Nothing
        Dim temp As String = Nothing
        Dim singleRange As Int64
        If _field.size() < 2 Then
            singleRange = displayRange
        Else
            singleRange = displayRange \ _field.size()
        End If
        rtn = "{fieldCount}" + _field.size().ToString
        For i = 0 To min(_field.size() - 1, convertor.cast(Of Int64)(displayRange))
            buff = take(i)
            convertString(System.Text.Encoding.UTF8.GetString(buff.data, 0, min(buff.size, singleRange)), temp)
            rtn += character.fullStop
            rtn += character.fullStop
            rtn += character.fullStop
            rtn += character.sheffer
            rtn += character.leftBrace + buff.size.ToString + character.rightBrace + temp
        Next
        Return rtn
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim rtn As nettransData = Nothing
        rtn = New nettransData(Me)

        Return rtn
    End Function

    Public Shared Sub unittest()
        Dim testsize As Int32 = 100000
        Dim d As nettransData = Nothing
        d = New nettransData
        Dim i As Int32
        perfCheckPoint(, "begin", True)
        For i = 0 To testsize
            d.insert("abc")
        Next
        perfCheckPoint(, "finish insert " + Convert.ToString(testsize + 1) + " 'abc' to d", True)
        Dim buff() As Byte = Nothing
        Dim size As Int32
        d.export(buff, size)
        perfCheckPoint(, "finish export from d", True)
        d.clear()
        perfCheckPoint(, "finish d.clear()", True)
        d.import(buff)
        perfCheckPoint(, "finish d.import(buff)", True)
        assert(testsize + 1 = d.fieldCount, "testsize+1<>d.fieldCount")
        For i = 0 To testsize
            assert(d.take(Of String)(i) = "abc", "d.take(Of String)(i) <> 'abc'")
        Next
        perfCheckPoint(, "finish d.take", True)
    End Sub

End Class
#End If
